var Api = require('../../../../utils/500pxApi.js');

var featureMap = {'Editor': 'editors', 'Today': 'fresh_today', 'Week': 'fresh_week'};

Page({
  data: {
    title: 'Explore',
    photos: [],
    feature: 'fresh_today',
    loading: true,
    hasMore: true,
    equalOne: false,
    rpp: 20,
    featureOptionHidden: true,
    featuresOptions: ['Editor', 'Today', 'Week']
  },
  lookPhoto: function(event) {
    var id = event.currentTarget.id,
      url = '../detail/detail?id=' + id;
    wx.navigateTo({
      url: url
    })
  },
  showFeatureOptions: function(e) {
    this.setData({
      featureOptionHidden: !this.data.featureOptionHidden
    })
  },
  changeFeatures: function(e) {
    this.setData({
      featureOptionHidden: !this.data.featureOptionHidden
    })
  },
  chooseFeature:function(e){
    console.log(featureMap[e.currentTarget.dataset.name]);
    this.setData({
      feature: featureMap[e.currentTarget.dataset.name],
      rpp: 20,
      photos:[],
      featureOptionHidden:true
    });
    this.initData(this.data.feature);
  },
  initData: function(f){
    this.fetchData();
  },
  loadMore: function(e) {
    console.log('down');
    if (this.data.hasMore) {
      this.fetchData();
    }
  },
  fetchData: function() {
    var that = this;

    var theRPP = that.data.rpp;
    console.log(theRPP);
    wx.request({
      url: Api.getPhotos(),
      data: {
        feature: this.data.feature,
        consumer_key: Api.getConsumerKey(),
        sort: 'votes_count',
        sort_direction: 'desc',
        image_size: '3',
        include_store: 'store_download',
        include_states: 'voted',
        rpp: theRPP
      },
      success: function(res) {
        console.log(res);
        var fetchedData = res.data.photos || [];
        var newData = that.data.photos;
        if (typeof res.data == "string"){
          console.log("数据格式异常，即将尝试解析。。");
          let data;
          try{
            data = JSON.parse(res.data.substr(1, res.data.length));
            fetchedData = data.photos;
            console.log("解析成功，数据长度：" + data.length);
          }catch(e){
            console.log("解析失败，错误：" + e);
          }
        }
        if (!fetchedData && fetchedData.length == 0){
          console.log("获取数据失败");
          that.setData({loading: false})
          return;
        }
        newData.push.apply(newData, fetchedData.slice(theRPP - 20, theRPP));

        var hasMore = true;
        var newRPP = theRPP + 20;
        var equalOne = false;
        if (fetchedData.length < theRPP) {
          hasMore = false;
          newRPP = fetchedData.length;
        } else if (that.equalOne) {
          hasMore = false;
          newRPP = fetchedData.length;
        } else {
          equalOne = true;
        }

        that.setData({
          photos: newData,
          loading: false,
          hasMore: hasMore,
          equalOne: equalOne,
          rpp: newRPP
        })
      },
      error:(res) =>{
        console.log("加载失败");
      },
      complete:()=>{
        console.log("请求结束");
      }
    })
  },
  onLoad: function () {
    wx.hideLoading();
    console.log('load explore');
    this.initData(this.data.feature);
  }
})
